////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "TextureHull.hpp"

#include <SFML/OpenGL.hpp>

#include <SmegTools/Debug/DebugNew.hpp>

namespace smeg {
    namespace light {
    
////////////////////////////////////////////////////////////
/// Default Constructor
////////////////////////////////////////////////////////////
TextureHull::TextureHull(   const sf::Vector2f& _Center, 
                            float _fAngleDeg, 
                            bool _bIsStatic,
                            ShadowCasterEntity* _pParent ) :
    Hull( _Center, _fAngleDeg, _bIsStatic, _pParent )
{
}
        
////////////////////////////////////////////////////////////
/// Constructor with a texture filename
////////////////////////////////////////////////////////////
TextureHull::TextureHull(   const std::string& _TextureFilename,
                            const sf::Vector2f& _Center, 
                            float _fAngleDeg, 
                            bool _bIsStatic,
                            ShadowCasterEntity* _pParent ) :
    Hull( _Center, _fAngleDeg, _bIsStatic, _pParent )
{
    SetTexture( _TextureFilename );
}
    
////////////////////////////////////////////////////////////
/// Copy Constructor
////////////////////////////////////////////////////////////
TextureHull::TextureHull( const Hull& _Copy ) :
    Hull( _Copy )
{
}
    
////////////////////////////////////////////////////////////
/// Is the given point inside the hull
////////////////////////////////////////////////////////////
bool TextureHull::IsPointInHull( const sf::Vector2f& _p ) const {
    return false;
}

////////////////////////////////////////////////////////////
/// Render the vertices
////////////////////////////////////////////////////////////
void TextureHull::RenderVertices() const {
    sf::Texture::bind( &m_Texture );

    glBegin( GL_QUADS );
        glTexCoord2i( 0, 0 ); glVertex3f( m_pVertices[0].x, m_pVertices[0].y, 0.0f );
        glTexCoord2i( 1, 0 ); glVertex3f( m_pVertices[1].x, m_pVertices[1].y, 0.0f );
        glTexCoord2i( 1, 1 ); glVertex3f( m_pVertices[2].x, m_pVertices[2].y, 0.0f );
        glTexCoord2i( 0, 1 ); glVertex3f( m_pVertices[3].x, m_pVertices[3].y, 0.0f );
    glEnd();

    sf::Texture::bind( NULL );
}

////////////////////////////////////////////////////////////
/// Compute the texture quad to render
////////////////////////////////////////////////////////////
void TextureHull::ComputeQuad() {
    m_uNumVertices = 4;
    if (!m_pOriginalVertices) {
        delete[] m_pOriginalVertices;
        delete[] m_pVertices;
        m_pVertices = NULL;
        m_pOriginalVertices = new sf::Vector2f[m_uNumVertices];
    }

    m_pOriginalVertices[0] = sf::Vector2f( -m_HalfSize.x, -m_HalfSize.y );
    m_pOriginalVertices[1] = sf::Vector2f( m_HalfSize.x, -m_HalfSize.y );
    m_pOriginalVertices[2] = sf::Vector2f( m_HalfSize.x, m_HalfSize.y );
    m_pOriginalVertices[3] = sf::Vector2f( -m_HalfSize.x, m_HalfSize.y );

    ComputeHull();
}

/*********************** Accessors ************************/
bool TextureHull::SetTexture( const std::string& _TextureFilename ) {
    if ( ( "" != _TextureFilename ) && m_Texture.loadFromFile( _TextureFilename ) ) {
        
        SetSize( sf::Vector2f( (float)m_Texture.getSize().x, (float)m_Texture.getSize().y  ) );

        return true;
    }

    return false;
}

void TextureHull::SetSize( const sf::Vector2f& _size ) {
    m_Size = _size;
    m_HalfSize = m_Size * 0.5f;

    ComputeQuad();
}

void TextureHull::SetRepeated( bool _bRepeated ) {
    m_Texture.setRepeated( _bRepeated );
}

bool TextureHull::IsRepeated() const {
    return m_Texture.isRepeated();
}
    
void TextureHull::SetSmooth( bool _bSmooth ) {
    m_Texture.setSmooth( _bSmooth );
}

bool TextureHull::IsSmooth() const {
    return m_Texture.isSmooth();
}

#ifdef _DEBUG
void TextureHull::DebugRender( const sf::Color& _Color ) {
}
#endif // _DEBUG
    } // smeg::light
} // smeg
